﻿using IPMulticastLib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace IPMulticastTestProject
{
    
    
    /// <summary>
    ///This is a test class for IPMulticasterTest and is intended to contain all IPMulticasterTest Unit Tests
    ///</summary>
	[TestClass()]
	public class IPMulticasterTest
	{
		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion

		/// <summary>
		///A test for Send
		///</summary>
		[TestMethod()]
		public void SendTest()
		{
			string multicastIP = "234.5.5.1"; // The IP of the multicast group you want to create or join.
			int multicastPort = 5555; // The Port of the multicast group you want to create or join
			int multicastTTL = 4; // The TimeToLive (number of router hops to allow until the message is droped)
			bool echo = true;
			IPMulticaster target = new IPMulticaster(multicastIP, multicastPort, multicastTTL, echo); // TODO: Initialize to an appropriate value

			System.Threading.ManualResetEvent MessageReceivedEvent = new System.Threading.ManualResetEvent(false);
			bool MessageReceived = false;
			string message = "Test Message Sent.";

			// handle the event with an anon delegate so we can stay inside the Test Method.		 
			target.MessageReceived += delegate
			{
				MessageReceived = true;
				MessageReceivedEvent.Set();
			};

			target.Send("Test Message Sent.");
			MessageReceivedEvent.WaitOne(5000, false);
			
			Assert.IsTrue(MessageReceived);
		}


		/// <summary>
		///A test for IPMulticaster Constructor. Test passes if target is instance of type IPMulticaster.
		///</summary>
		[TestMethod()]
		public void IPMulticasterConstructorTest()
		{
			//IPMulticastLib.IPMulticaster mCaster = new IPMulticastLib.IPMulticaster("234.5.5.1", 5555, 4);
			string multicastIP = "234.5.5.1"; // The IP of the multicast group you want to create or join.
			int multicastPort = 5555; // The Port of the multicast group you want to create or join
			int multicastTTL = 4; // The TimeToLive (number of router hops to allow until the message is droped)
			bool echo = false;
			IPMulticaster target = new IPMulticaster(multicastIP, multicastPort, multicastTTL, echo);

			Assert.IsInstanceOfType(target, typeof(IPMulticaster));
		}

		/// <summary>
		///A test for IPMulticaster Constructor passing a bad IP. Test passes if the instance is not created due to an exception.
		///</summary>
		[TestMethod()]
		public void IPMulticasterConstructor_BadIPTest()
		{
			//IPMulticastLib.IPMulticaster mCaster = new IPMulticastLib.IPMulticaster("234.5.5.1", 5555, 4);
			string multicastIP = "Not An IP Address"; // The IP of the multicast group you want to create or join.
			int multicastPort = 5555; // The Port of the multicast group you want to create or join
			int multicastTTL = 4; // The TimeToLive (number of router hops to allow until the message is droped)
			bool echo = false;
			try
			{
				IPMulticaster target = new IPMulticaster(multicastIP, multicastPort, multicastTTL, echo);
			}
			catch (Exception ex)
			{
				Assert.IsInstanceOfType(ex, typeof(Exception));
				return;
			}
			Assert.Fail();
		}

		/// <summary>
		///A test for IPMulticaster Constructor passing an empty IP. Test passes if the instance constructor throws exception.
		///</summary>
		[TestMethod()]
		public void IPMulticasterConstructor_EmptyIPTest()
		{
			//IPMulticastLib.IPMulticaster mCaster = new IPMulticastLib.IPMulticaster("234.5.5.1", 5555, 4);
			string multicastIP = ""; // The IP of the multicast group you want to create or join.
			int multicastPort = 5555; // The Port of the multicast group you want to create or join
			int multicastTTL = 4; // The TimeToLive (number of router hops to allow until the message is droped)
			bool echo = false;
			try
			{
				IPMulticaster target = new IPMulticaster(multicastIP, multicastPort, multicastTTL, echo);
			}
			catch (Exception ex)
			{
				Assert.IsInstanceOfType(ex, typeof(Exception));
				return;
			}
			Assert.Fail();
		}

		/// <summary>
		///A test for IPMulticaster Constructor passing a null IP. Test passes if the instance is not created due to an exception.
		///</summary>
		[TestMethod()]
		public void IPMulticasterConstructor_NullIPTest()
		{
			//IPMulticastLib.IPMulticaster mCaster = new IPMulticastLib.IPMulticaster("234.5.5.1", 5555, 4);
			string multicastIP = null; // The IP of the multicast group you want to create or join.
			int multicastPort = 5555; // The Port of the multicast group you want to create or join
			int multicastTTL = 4; // The TimeToLive (number of router hops to allow until the message is droped)
			bool echo = false;
			try
			{
				IPMulticaster target = new IPMulticaster(multicastIP, multicastPort, multicastTTL, echo);
			}
			catch (Exception ex)
			{
				Assert.IsInstanceOfType(ex, typeof(Exception));
				return;
			}
			Assert.Fail();
		}














		/// <summary>
		///A test for OnMessageReceived
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPMulticastLib.dll")]
		public void OnMessageReceivedTest()
		{
			PrivateObject param0 = null; // TODO: Initialize to an appropriate value
			IPMulticaster_Accessor target = new IPMulticaster_Accessor(param0); // TODO: Initialize to an appropriate value
			MessageReceivedEventArgs e = null; // TODO: Initialize to an appropriate value
			target.OnMessageReceived(e);

			Assert.Inconclusive("A method that does not return a value cannot be verified.");
		}
	}
}
